﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.Model;
using TelnetRouterApplication.Control;

namespace TelnetRouterApplication.View
{
    public partial class StaticRouteForm : UserControl
    {
        private List<Route> _routes;
        public List<Route> Routes
        {
            get { return _routes; }
            set { _routes = value; }
        }

        public StaticRouteForm()
        {
            InitializeComponent();
            RefreshRouteList();
        }

        private void RefreshRouteList()
        {
            Routes = CommandHandler.GetInstance().GetStaticRouteList();
            setRouteList();
        }

        private void setRouteList()
        {
            routeListBox.BeginUpdate();
            routeListBox.Items.Clear();
            foreach (Route route in Routes)
            {
                routeListBox.Items.Add(route);
            }
            routeListBox.EndUpdate();
        }

        private void addRouteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                enableButtons(false);
                Route routeToInsert = new Route(tbNetwork.Text, tbMask.Text, tbNextHop.Text);
                bool success = CommandHandler.GetInstance().AddRoute(routeToInsert);
                if (!success)
                {
                    MessageBox.Show("Erro ao inserir STATIC");
                }
                RefreshRouteList();
            }
            finally
            {
                enableButtons(true);
            }
        }

        private void removeRouteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                enableButtons(false);
                Route routeToDelete = (Route)routeListBox.SelectedItem;
                if (routeToDelete != null)
                {
                    CommandHandler.GetInstance().DeleteRoute(routeToDelete);
                }
                RefreshRouteList();
            }
            finally
            {
                enableButtons(true);
            }
        }

        private void enableButtons(bool enable)
        {
            addRouteBtn.Enabled = enable;
            removeRouteBtn.Enabled = enable;
        }
    }
}
